package com.shopping.controller.front;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.shopping.commons.controller.BaseController;
import com.shopping.commons.response.MessageResp;
import com.shopping.commons.util.CommonCode;
import com.shopping.model.Commodity;
import com.shopping.param.CommodityParam;
import com.shopping.response.CommodityResp;
import com.shopping.response.UserResp;
import com.shopping.service.IAddressService;
import com.shopping.service.ICommodityService;

/**
 * 商品的   Conroller 前台
 * @author Administrator
 *
 */
@Controller
@RequestMapping("front/commodity") 
public class FrontCommodityController extends BaseController{

	@Autowired
	private ICommodityService commodityService;
	
	@Autowired
	private IAddressService addressService;
	
	/**
	 * 购买商品的页面生成
	 * @param request
	 * @param param
	 * @return
	 */
	@RequestMapping("buy/{ids}")
	@ResponseBody  ///这个是吧MessageResp转成  json 的注解   普遍就是   对应  ajax 的请求    
	public ModelAndView buy(HttpServletRequest request,@PathVariable("ids")String id){
		UserResp retUser=this.getUser(request);
		if(retUser==null){
			return new ModelAndView("redirect:../../../views/front/index.jsp");
		}
		ModelAndView mv=new ModelAndView("views/front/login/order");
		//查询收货地址列表
		mv.addObject("addressList",addressService.findAddressByUser(retUser.getUserid()));
		mv.addObject("orderDetails",commodityService.findByIds(id));
		return mv;
	}
	
	/**
	 * 查询 商品
	 * @param request
	 * @param param
	 * @return
	 */
	@RequestMapping("select/{mthod}")
	@ResponseBody  
	public MessageResp select(@PathVariable("mthod")String mthod,HttpServletRequest request,CommodityParam param){
		MessageResp resp=new MessageResp();
		resp.setCode(CommonCode.SUCCESS);
		resp.setMsg("查询成功");
		List<CommodityResp> retList=commodityService.select(mthod,param);
		resp.setObj(retList);
		return resp;
	}
	
	/**
	 * 修改商品库存
	 * @param request
	 * @param param
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody 
	public MessageResp update(HttpServletRequest request,Commodity param){
		return commodityService.update(param);
	}

}
